package org.lai.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.lai.mapper.StudentMapper;
import org.lai.pojo.PageResult;

import org.lai.pojo.Student;
import org.lai.pojo.StudentQueryParm;
import org.lai.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> getList(StudentQueryParm student) {
        if(student.getPage() == null){
            student.setPage(1);
        }
        if (student.getPageSize() == null){
            student.setPageSize(10);
        }
        // 设置PageHelper插件的分页参数
        PageHelper.startPage(student.getPage(),student.getPageSize());
        // 执行查询
        List<Student> list = studentMapper.getList(student);
        // 封装结果
        Page<Student> p = (Page<Student>) list;
        // 返回结果
        return new PageResult<Student>(p.getTotal(),p.getResult());
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }

    @Override
    public void updateStudent(Integer id, Integer score) {
        studentMapper.updateScore(id,score);
    }
}
